-- Thanks to plusmedic!
--[[Function: findStat(item, sStr)
	Usage: findStat(item, "Armor Class")
	Description: Searches through an items stats and returns a value.
		If it doesn't find the stat it returns -1
	Note: Can search by actual name or stat name (i.e., Armor Class or armorclass).
		More info here: http://redvex.d2help.com/viewtopic.php?f=15&t=3238
	]]
function findStat(item, sStr)
	for i = 0, item.stats:size() - 1 do
		if item.stats[i].Stat.Stat == sStr then
			return item.stats[i].Value
		elseif item.stats[i].Stat.Name == sStr then
			return item.stats[i].Value
		end
	end

	for i = 0, item.mods:size() - 1 do
		if item.mods[i].Stat.Stat == sStr then
			return item.mods[i].Value
		elseif item.mods[i].Stat.Name == sStr then
			return item.mods[i].Value
		end
	end

	return -1
end

-- Thanks to plusmedic!
--[[Function: defenseCalc(item)
	Usage: defenseCalc(item)
	Description: Calculates the final defense value of an item after modifiers.
	If the item doesn't have defense it returns -1
	Note: None
	]]
function defenseCalc(item)
	local basedef, edef, adef = 0, 0, 0

	for i = 0, item.stats:size() - 1 do
		if item.stats[i].Stat.Stat == "armorclass" then
			basedef = item.stats[i].Value
			break
		end
	end

	if basedef then
		for i = 0, item.mods:size() - 1 do
			if item.mods[i].Stat.Stat == "item_armor_percent" then
				edef = item.mods[i].Value
			elseif item.mods[i].Stat.Stat == "armorclass" then
				adef = item.mods[i].Value
			end
		end
	else
		return -1
	end

	return math.floor(basedef * (edef / 100 + 1) + adef)
end

-- Thanks to Mixam!
--[[Function: hasCertainSockets(item, sockets)
	sockets: is a string with all the sockets you want in it use ""
	Returns: true if the right number of sockets is found
	ex: hasCertainSockets(item, "045") would check if its 0 or 4 or 5 sockets  
	]]
function hasCertainSockets(item, sockets)
	local nsockets = 7
	
	if item.flags.Socketed == false then
		nsockets = 0
	else
		for i = 0, item.stats:size() - 1 do
			if item.stats[i].Stat.Name == "Sockets" then
				nsockets = item.stats[i].Value
			end
		end
	end
	
	for i = 1, string.len(sockets) do 
		if (string.byte(sockets, i) - 48) == nsockets then
			 return true
		end   
	end

	return false
end   

--[[Function: hasRes(item, percent, numres)
	percent: value, minimum resistance
	numres: value, number of resistances
		*poison only counts if numres==4
	Returns: true if item has numres >= percent
	ex: hasRes(item, 20, 2) would return true if the item has 2 resists
		(excluding poison) and they are both 20+
	]]
function hasRes(item, percent, numres)
	local res = 0

	for i = 0, item.mods:size() - 1 do
		if item.mods[i].Stat.Type == StatType.FireResist and
				item.mods[i].Value >= percent then
			res = res + 1
		elseif item.mods[i].Stat.Type == StatType.LightResist and
				item.mods[i].Value >= percent then
			res = res + 1
		elseif item.mods[i].Stat.Type == StatType.ColdResist and
				item.mods[i].Value >= percent then
			res = res + 1
		elseif numres == 4 and item.mods[i].Stat.Type == StatType.PoisonResist and
				item.mods[i].Value >= percent then
			res = res + 1
		end
	end

	if numres <= res then
		return true
	end

	return false
end

-- please write a general description for this, ilikebigtits
-- I have a general idea of how to describe it, but would prefer your own words
function countAllres(item)
	local rescount, minres, sum = 0, 0, 0
	local cold, fire, light, poison = 0, 0, 0, 0

	for i=0,item.mods:size()-1 do
		if item.mods[i].Stat.Type == StatType.FireResist then
			fire=item.mods[i].Value
			rescount = rescount + 1
			sum=sum+item.mods[i].Value
			if rescount == 1 or minres > item.mods[i].Value then
				minres = item.mods[i].Value
			end
		elseif item.mods[i].Stat.Type == StatType.LightResist then
			light=item.mods[i].Value
			rescount = rescount + 1
			sum=sum+item.mods[i].Value
			if rescount == 1 or minres > item.mods[i].Value then
				minres = item.mods[i].Value
			end
		elseif item.mods[i].Stat.Type == StatType.PoisonResist then
			poison=item.mods[i].Value
			rescount = rescount + 1
			sum=sum+item.mods[i].Value
			if rescount == 1 or minres > item.mods[i].Value then
				minres = item.mods[i].Value
			end
		elseif item.mods[i].Stat.Type == StatType.ColdResist then
			cold=item.mods[i].Value
			rescount = rescount + 1
			sum=sum+item.mods[i].Value
			if rescount == 1 or minres > item.mods[i].Value then
				minres = item.mods[i].Value
			end
		end
	end

	ret={}
	ret.All		= minres
	ret.Fire		= fire
	ret.Cold		= cold
	ret.Light	= light
	ret.Poison	= poison
	ret.Sum		= sum

	return ret
end

--[[Function: hasSkill(item, skill, value)
	skill: literal SkillType.x or SkillTab.x
	value: value, minimum value of skill level
	Returns: true if item has skill >= value
	*Currently has very limited usage
	ex1: hasSkill(item, SkillType.Enchant, 3) returns true if item has +3+enchant
	ex2: hasSkill(item, SkillTab.PaladinCombat, 3) returns true if item has +3+pcomb
	]]
function hasSkill(item, skill, value)
	for i = 0, item.mods:size() - 1 do
		if item.mods[i].Stat.Type == StatType.SingleSkill and
				item.mods[i].Skill == skill and
				item.mods[i].Value >= value then
			return true
		elseif item.mods[i].Stat.Type == StatType.SkillTabBonus and
				item.mods[i].Tab == skill and
				item.mods[i].Value >= value then
			return true
		end
	end

	return false
end